#!/bin/bash
if [ "$(id -u)" == "0" ]
then 
        sudo apt-get update -y
        sudo apt-get install -y software-properties-common
        sudo apt-get install -y openresolv
        sudo apt-get install -y qrencode
        
      

    sudo add-apt-repository -y ppa:wireguard/wireguard
    sudo apt-get update -y
    sudo apt-get install -y wireguard curl
    sleep 10
    echo "系统更新完成，并且安装好组件"
      sleep 3
      echo "内核开始"
    sudo echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
    sysctl -p
    echo "1"> /proc/sys/net/ipv4/ip_forward
    echo "内核转发成功"
   
    mkdir /etc/wireguard
    sleep 3
    echo "进入目录"
    cd /etc/wireguard
    wg genkey | tee sprivatekey | wg pubkey > spublickey
    wg genkey | tee cprivatekey | wg pubkey > cpublickey
   s1=$(cat sprivatekey)
    s2=$(cat spublickey)
    c1=$(cat cprivatekey)
    c2=$(cat cpublickey)
    serverip=$(curl ipv4.icanhazip.com)
    
    eth=$(ls /sys/class/net | awk '/^e/{print}')
   sleep 3
echo "创建服务端网络配置文件"
    sudo echo -e "
[Interface]
PrivateKey = $s1
Address = 10.0.0.1/24 
PostUp   = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $eth -j MASQUERADE
ListenPort = 51820
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey = $c2
AllowedIPs = 10.0.0.2/32" >>/etc/wireguard/wg0.conf
sleep 3
echo "配置客户端文件"

sudo echo -e "

[Interface]
PrivateKey = $c1
Address = 10.0.0.2/24 
DNS = 8.8.8.8
MTU = 1420

[Peer]
PublicKey = $s2
Endpoint = $serverip:51820
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25
" >> /etc/wireguard/xiaolu.conf

sleep 3
  echo "安装手机插件"

    




    sleep 3
apt install rinetd -y
echo -e "rinetd 安装成功"
rm -rf /etc/rinetd.conf
touch /etc/rinetd.conf
echo -e "0.0.0.0 31400 10.10.10.2 31400
         0.0.0.0 31401 10.10.10.2 31401
         0.0.0.0 31402 10.10.10.2 31402
         0.0.0.0 31403 10.10.10.2 31403
         0.0.0.0 31404 10.10.10.2 31404
         0.0.0.0 31405 10.10.10.2 31405
         0.0.0.0 31406 10.10.10.2 31406
         0.0.0.0 31407 10.10.10.2 31407
         0.0.0.0 31408 10.10.10.2 31408
         0.0.0.0 31409 10.10.10.2 31409
         0.0.0.0 31410 10.10.10.2 31410" >> /etc/rinetd.conf

rinetd -c /etc/rinetd.conf

#添加开机自动重启服务
chmod 777 /etc/rc.local 
chmod 777 /root/wire/startup.sh
echo -e "/root/wire/startup.sh >/tmp/xiaolu.log 2>&1" >>/etc/rc.local
cp -R /etc/wireguard/xiaolu.conf /root
sudo wg-quick up wg0
echo -e "服务器架设成功，请验收 我是笨蛋刘晓陆"
else 
echo  "不是root登录无法运行,请用root登录"
fi